package com.atguigu.petstore.member.rpc;
import java.math.BigDecimal;

import com.atguigu.petstore.common.result.Result;
import com.atguigu.petstore.member.entity.MemberInfo;
import com.atguigu.petstore.member.service.MemberInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author lfy
 * @Description 对于用户系统的所有远程调用接口，都定义在这里
 * @create 2023-09-27 14:17
 */
@Tag(name = "RPC-会员接口")
@RequestMapping("/rpc/member")
@RestController
public class MemberRpcController {


    @Autowired
    MemberInfoService memberInfoService;

    @Operation(summary = "使用手机号自动注册一个会员")
    @PostMapping("/register/{phone}")
    public Result<MemberInfo> register(@PathVariable("phone") String phone){

        MemberInfo info = new MemberInfo();
        info.setLoginName(phone); //数据库有唯一约束
        info.setLoginPwd("");
        info.setNickName("会员_"+phone);
        info.setHeaderImg("/1.jpg");
        info.setGender("");
        info.setMemberPoints(new BigDecimal("0"));

        memberInfoService.save(info);

        return Result.ok(info);
    }


    @Operation(summary = "按照手机号查询会员信息")
    @GetMapping("/info/{phone}")
    public Result<MemberInfo> getMemeberByPhone(@PathVariable("phone") String phone){


        MemberInfo info = memberInfoService.lambdaQuery()
                .eq(MemberInfo::getLoginName, phone)
                .one();

        return Result.ok(info);
    }
}
